import { DB } from "@/lib/db";
import { faker } from "@faker-js/faker";
import { userRoles, UserRoleSchema } from "@supersavely/db/schema";
import { db } from "../seed";

const userRoleMock = async () => {
  const data: Omit<Extract<UserRoleSchema, { mode: "addUserRole" }>, "mode">[] =
    [];

  const [usersData, rolesData] = await Promise.all([
    db.query.users.findMany(),
    db.query.roles.findMany(),
  ]);

  usersData.map((user) => {
    data.push({
      userId: user.id,
      roleId: faker.helpers.arrayElement(rolesData).id,
    });
  });
  return data;
};

export async function seed(db: DB) {
  console.log("start seeding userRoles");
  const userRolesData = await userRoleMock();
  await db.insert(userRoles).values(userRolesData);
}
